﻿using System.Threading.Tasks;
using CRM.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace CRM.Controllers
{
    [AllowAnonymous]
    public class LogoController : BaseController
    {
        private readonly MyDbContext _dbContext;
        private readonly OemService _oemService;

        public LogoController(MyDbContext dbContext, OemService oemService)
        {
            _dbContext = dbContext;
            _oemService = oemService;
        }

        [Route("/logo.png")]
        public async Task<IActionResult> Logo()
        {
            var oem = await _oemService.GetCurrentOem();
            // 默认图标
            if (null == oem)
            {
                return Redirect("/img/logo.png");
            }

            if (string.IsNullOrEmpty(oem.LogoUrl))
            {
                return null;
            }

            return Redirect(oem.LogoUrl);
        }
        
        [Route("/favicon.ico")]
        public async Task<IActionResult> Icon()
        {
            var oem = await _oemService.GetCurrentOem();
            // 默认图标
            if (null == oem)
            {
                return Redirect("/img/favicon.ico");
            }

            if (string.IsNullOrEmpty(oem.IconUrl))
            {
                return null;
            }

            return Redirect(oem.IconUrl);
        }
        
        /// <summary>
        /// 当前oem
        /// </summary>
        /// <returns></returns>
        [Route("/api/oem")]
        public async Task<ResultStruct> Oem()
        {
            var oem = await _oemService.GetCurrentOem();
            var indexContent = string.IsNullOrEmpty(oem.IndexContent) ? "大数据智能搜索,让外贸更轻松！" : oem.IndexContent;
            return Success(new {oem.Id, oem.Title, oem.Name, indexContent, oem.ShowLoginLogo, oem.ShowIndexLogo, oem.ShowIndexContent, oem.IndexUiSettings});
        }
        [Route("/api/download/import")]
        public  IActionResult DownFile()
        {
            var filePath = "template/excel/import.xlsx";
            return File(new System.IO.FileStream(filePath, System.IO.FileMode.Open), "application/octet-stream", System.IO.Path.GetFileName(filePath));
        }
    }
}